/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class Question {
    private final String desc;
    private final List<String> opts,correctOps;
    private final List<Integer> corrects;
    private final int type;
    public static final int Single_Selection = 1, Multiple_Selection = 2;
    
    public Question(String s,List<String> o,List<Integer> c,int t) {
        desc = s;
        opts = Collections.unmodifiableList(o);
        corrects = Collections.unmodifiableList(c);
        type = t;
        correctOps = new ArrayList<String>(corrects.size());
        for(Integer i : corrects)
            correctOps.add(opts.get(i));
    }
    
    public boolean isCorrect(List<String> answers) {
        return (answers.containsAll(correctOps) && (answers.size()==correctOps.size()));
    }
    
    public List<String> getOptions() { return opts; }
    public List<Integer> getCorrectAnswersNumber() { return corrects; }
    public List<String> getCorrectAnswers() { return correctOps; }
    public String getDescription() { return desc; }
    public int getType() { return type; }
    public boolean isSingleSelection() { return type == Single_Selection; }
    public boolean isMultipleSelection() { return type == Multiple_Selection; }
    
    @Override
    public String toString() {
        StringBuilder s =  new StringBuilder();
        s.append(type).append(sep);
        s.append(desc).append(sep);
        s.append(opts.size()).append(sep);
        for(String st : opts)
            s.append(st).append(sep);
        for(Integer i : corrects)
            s.append(i).append(sep);
        s.replace(s.length()-sep.length(), s.length() + 1, "");
        return s.toString();
    }
    
    public static Question fromString(String s) {
        String[] st = s.split(sep);
        int type = Integer.parseInt(st[0]);
        String d = st[1];
        int n;
        ArrayList<String> opts = new ArrayList<String>(n = Integer.parseInt(st[2]));
        ArrayList<Integer> co = new ArrayList<Integer>();
        for(int i=3; i<=n+2; i++)
            opts.add(st[i]);
        for(int i = n+3 , k = st.length ; i<k ; i++)
            co.add(Integer.decode(st[i]));
        return new Question(d,opts,co,type);
    }
    
    private static final String sep = "&o1"; 
}
